//
//  EditTextMoreScrollView.m
//  QRCode
//
//  Created by zhibao on 2023/2/17.
//

#import "SelectTextMoreScrollView.h"
#import "LazyScrollView.h"
#import "SelectTextSubView.h"
@interface SelectTextMoreScrollView ()<LazyScrollViewDelegate>{
    int startIndex;
    NSArray *contentArray;
    LazyScrollView *moreScrollView;
    QMUITextField *textField;
}

@end

@implementation SelectTextMoreScrollView


- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        [self initContent];
    }
    return self;
}

-(void)initContent{
    self.backgroundColor = UIColor.whiteColor;
    startIndex = 0;
    contentArray = [NSArray arrayWithObjects:
                    @{@"title":@"",@"type":@"1",@"img":@""},
                    nil];
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelBtn setImage:[UIImage systemImageNamed:@"xmark"] forState:UIControlStateNormal];
    cancelBtn.tintColor = MyColor(@"666666");
    cancelBtn.tag = 100;
    [cancelBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:cancelBtn];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(5);
        make.left.mas_equalTo(10);
        make.width.height.mas_equalTo(pixw(40));
    }];
    
//    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [sureBtn setImage:[UIImage systemImageNamed:@"paperplane"] forState:UIControlStateNormal];
//    sureBtn.tintColor = MYBlue;
//    sureBtn.tag = 101;
//    [sureBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
//    [self addSubview:sureBtn];
//    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(5);
//        make.left.mas_equalTo(MY_Screen_W - 10 - 40);
//        make.width.height.mas_equalTo(40);
//    }];
    
    textField = [[QMUITextField alloc]init];
    textField.layer.cornerRadius = pixw(18);
    textField.layer.borderColor = MyColor(@"#D8D8D8").CGColor;
    textField.layer.borderWidth = pixw(0.5);
    textField.font = MY_SCALE_RegularFONT(14);
    textField.textColor = MyColor(@"333333");
    textField.placeholderColor = MyColor(@"999999");
    textField.maximumTextLength = 15;
    textField.textInsets = UIEdgeInsetsMake(0, 20, 0, 0);
    textField.placeholder = LocalizedString(@"请输入文本内容（15字以内）");
    [self addSubview:textField];
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(pixw(15));
        make.width.mas_equalTo(MY_Screen_W - pixw(30));
        make.top.mas_equalTo(pixw(58));
        make.height.mas_equalTo(pixw(36));
    }];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(pixw(15), pixw(111), pixw(300), pixw(16))];
    titleLabel.font = MY_SCALE_RegularFONT(12);
    titleLabel.text = LocalizedString(@"边框颜色");
    titleLabel.textColor = [UIColor colorWithHexString:@"#666666"];
    [self addSubview:titleLabel];
    
    moreScrollView = [[LazyScrollView alloc]initWithFrame:CGRectMake(0, titleLabel.bottom, self.width, pixw(50)) delegate:self dataArray:contentArray withShowItmes:NO withDirection:2 withSpace:pixw(50)];
    [self addSubview:moreScrollView];
    moreScrollView.topScrollView.backgroundColor = [UIColor whiteColor];
    [moreScrollView.topScrollView setNormalColor:@"999999" withSelectColor:@"2666e4"];
}

-(void)btnClick:(UIButton *)sender{
    if(self.dealMoreBlock){
        if(sender.tag == 100){
            self.dealMoreBlock(NO,textField.text);
        }else{
            self.dealMoreBlock(YES,textField.text);
        }
    }
}

#pragma mark  LazyScrollViewDelegate
- (LazyScrollSubView *)lazyViewAtIndex:(int)index{
    SelectTextSubView *ingSubView = [[SelectTextSubView alloc]initWithFrame:CGRectMake(0, 0, MY_Screen_W, pixw(50))];
    __weak typeof(self) weakSelf = self;
    ingSubView.selectColorSubBlock = ^(NSDictionary * _Nonnull dic) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        if(![dic[@"value"] isEqualToString:CLEARFLAG] && self->textField.text.length < 1){
            [QMUITips showError:LocalizedString(@"请输入文本")];
            return;
        }
        
        NSMutableDictionary *temDic = [[NSMutableDictionary alloc]initWithDictionary:dic];
        temDic[@"text"] = self->textField.text;
        if(strongSelf.selectMoreBlock){
            strongSelf.selectMoreBlock(temDic);
        }
    };
    return ingSubView;
}

@end
